package com.easyschool.webservices;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import com.easyschool.model.bo.BoUser;
import com.easyschool.model.school.ConfirmClass;
import com.easyschool.model.user.ConfirmUser;
import com.easyschool.model.user.User;
import com.easyschool.tools.Constants;

@Path("/user")
public class UserWS {

	@Path("/getAllUsers")
	@GET
	@Produces({"application/xml", "application/json"})
	public List<User> getAllUsers() throws Exception{
		List<User> list = BoUser.getInstance().getAllUsers();
		return list;
	}
	
	@Path("/getUserById")
	@GET
	@Produces({"application/xml", "application/json"})
	public User getUserById(@QueryParam("idUser") String idUser) throws Exception{
		User user = BoUser.getInstance().getUserById(new Integer(idUser));
		return user;
	}
	
	
	@Path("/getAllUsersByFilter")
	@GET
	@Produces({"application/xml", "application/json"})
	public List<User> getAllUsersByFilter(@QueryParam("name") String name, @QueryParam("surname") String surname, @QueryParam("birthday")String birthday, @QueryParam("isProfessor")String isProfessor) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<User> list = BoUser.getInstance().getAllUsersByFilters(name, surname, new Date(sdf.parse(birthday).getTime()), Constants.YES.equals(isProfessor));
		return list;
	}
	
	@Path("/addUser")
	@POST
	@Produces({"application/xml", "application/json"})
    @Consumes("application/x-www-form-urlencoded")
	public User addUser(@FormParam("name") String name, @FormParam("surname") String surname, 
			@FormParam("birthday") String birthday, @FormParam("idClass") String idClass,  @FormParam("isProfessor") String isProfessor) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user = new User(null, name, surname, new Date(sdf.parse(birthday).getTime()), new Date(new java.util.Date().getTime()), new Integer(idClass), Constants.YES.equals(isProfessor));
		user.setId(BoUser.getInstance().addUser(user));
		return user;
	}
	
	@Path("/updateUser")
	@POST
	@Produces({"application/xml", "application/json"})
    @Consumes("application/x-www-form-urlencoded")
	public User updateUser(@FormParam("idUser") String id, @FormParam("name") String name, @FormParam("surname") String surname, 
			@FormParam("birthday") String birthday, @FormParam("idClass") String idClass, @FormParam("isProfessor") String isProfessor) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		User user = new User(new Integer(id), name, surname, new Date(sdf.parse(birthday).getTime()), new Date(new java.util.Date().getTime()), new Integer(idClass),  Constants.YES.equals(isProfessor));
		BoUser.getInstance().updateUser(user);
		return user;
	}
	
	@Path("/removeUser")
	@GET
	@Produces({"application/xml", "application/json"})
	public ConfirmUser removeUser(@QueryParam("id") Integer id) throws Exception{
		ConfirmUser confirm = new ConfirmUser();
		confirm.setId(id);
		BoUser.getInstance().removeUser(id);
		return confirm;
	}
	
	@Path("/addTeachClasses")
	@POST
	@Produces({"application/xml", "application/json"})
    @Consumes("application/x-www-form-urlencoded")
	public User addTeachClasses(@FormParam("idUser") String idUser, @FormParam("idClass") String idClass)throws Exception{
		BoUser.getInstance().addTeachClasses(new Integer(idUser), new Integer(idClass));
		return getUserById(idUser);
	}
	
	@Path("/removeTeachClasses")
	@GET
	@Produces({"application/xml", "application/json"})
	public ConfirmClass removeTeachClasses(@QueryParam("idClass") Integer idClass, @QueryParam("idUser") Integer idUser) throws Exception{
		ConfirmClass confirm = new ConfirmClass();
		confirm.setId(idClass);
		
		BoUser.getInstance().removeTeachClasses(idClass, idUser);
		
		return confirm;
	}
	
}
